﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.Skins;
using DevExpress.XtraBars.Ribbon;
using DevExpress.Utils.Drawing;
using System.IO;
using Entities;
using Datalayer;

namespace QLHoNgheo
{
    public partial class frmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        int _ID_nguoidung; // mã người dùng
        int _ID_loaind; // mã loại người dùng
        public frmMain(int ma_nguoidung, int ma_loaind)
        {
            InitializeComponent();
            InitSkinGallery();
            this._ID_nguoidung = ma_nguoidung;
            this._ID_loaind = ma_loaind;
        }
        #region SkinGallery
        void InitSkinGallery()
        {
            DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.BonusSkins).Assembly);
            DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.OfficeSkins).Assembly);
            SimpleButton imageButton = new SimpleButton();

            foreach (SkinContainer cnt in SkinManager.Default.Skins)
            {
                imageButton.LookAndFeel.SetSkinStyle(cnt.SkinName);
                GalleryItem gItem = new GalleryItem();
                int groupIndex = 0;
                if (cnt.SkinName.IndexOf("Office") > -1) groupIndex = 1;
                rgbiSkins.Gallery.Groups[groupIndex].Items.Add(gItem);
                gItem.Caption = cnt.SkinName;

                gItem.Image = GetSkinImage(imageButton, 32, 17, 2);
                gItem.HoverImage = GetSkinImage(imageButton, 70, 36, 5);
                gItem.Caption = cnt.SkinName;
                gItem.Hint = cnt.SkinName;
            }
        }
        Bitmap GetSkinImage(SimpleButton button, int width, int height, int indent)
        {
            Bitmap image = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(image))
            {
                StyleObjectInfoArgs info = new StyleObjectInfoArgs(new GraphicsCache(g));
                info.Bounds = new Rectangle(0, 0, width, height);
                button.LookAndFeel.Painter.GroupPanel.DrawObject(info);
                button.LookAndFeel.Painter.Border.DrawObject(info);
                info.Bounds = new Rectangle(indent, indent, width - indent * 2, height - indent * 2);
                button.LookAndFeel.Painter.Button.DrawObject(info);
            }
            return image;
        }
        private void rgbiSkins_Gallery_ItemClick(object sender, DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs e)
        {
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(e.Item.Caption);
            set_Skin(e.Item.Caption);
        }
        void set_Skin(string sk)
        {
            try
            {
                FileInfo f = new FileInfo(Application.StartupPath + @"\skin.cse");
                if (f.Exists)
                    f.Delete();
                File.WriteAllText(f.FullName, sk);
            }
            catch { }
        }
        string get_Skin()
        {
            try
            {
                FileInfo f = new FileInfo(Application.StartupPath + @"\skin.cse");
                if (f.Exists)
                    return File.ReadAllText(f.FullName);
                return "Blue";
            }
            catch
            {
                return "Blue";
            }
        }
        private void rgbiSkins_Gallery_InitDropDownGallery(object sender, DevExpress.XtraBars.Ribbon.InplaceGalleryEventArgs e)
        {
            e.PopupGallery.CreateFrom(rgbiSkins.Gallery);
            e.PopupGallery.AllowFilter = false;
            e.PopupGallery.ShowItemText = true;
            e.PopupGallery.ShowGroupCaption = true;
            e.PopupGallery.AllowHoverImages = false;
            foreach (GalleryItemGroup galleryGroup in e.PopupGallery.Groups)
                foreach (GalleryItem item in galleryGroup.Items)
                    item.Image = item.HoverImage;
            e.PopupGallery.ColumnCount = 2;
            e.PopupGallery.ImageSize = new Size(70, 36);
        }
        #endregion
        private void frmMain_Load(object sender, EventArgs e)
        {
            this.defaultLookAndFeel1.LookAndFeel.SkinName = get_Skin();
            load_data_begin();
        }

        // load dữ liệu ban đầu sau khi đăng nhập
        private void load_data_begin()
        {
            switch (_ID_loaind)
            {
                case 2: ribbonPageGroup_qlhn.Visible = true;
                        break;
                case 3: ribbonPageGroup_qlncc.Visible = true;
                        break;
                case 1:
                        ribbonPageGroup_ad.Visible = true;
                        ribbonPageGroup_qlncc.Visible = true;
                        ribbonPageGroup_qlhn.Visible = true;
                        break;
            }
            IQueryMgr iq = QueryMgr.GetDbInstance();
            USER ur = new USER();
            ur = iq.U_GetUSERByID(_ID_nguoidung);
            barStaticItem1.Caption ="Người dùng :"+ ur.FullName;
        }
        private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
        {
            // thoát
            Application.Exit();
        }
        public void OpenForm(string fopen, object []paras)
        {
            try
            {
                Boolean bCheck = true;
                foreach (Form f in this.MdiChildren)
                {
                    if (fopen == f.Name)
                    {
                        bCheck = false;
                        f.Activate();
                        break;
                    }
                }
                if (bCheck)
                {
                    Form f1 = GetFormByName(fopen, paras);
                    if (f1 != null)
                    {
                        f1.MdiParent = this;
                        f1.Show();
                    }
                }
            }
            catch
            {
            }
        }

        private Form GetFormByName(string fopen, object[] paras)
        {

                Type T = Type.GetType(fopen, false);
                try
                {
                    if (T == null)
                    {
                        string Fullname = Application.ProductName + "." + fopen;
                        T = Type.GetType(Fullname, true, true);
                    }
                    if (paras != null)
                    {
                        return (Form)Activator.CreateInstance(T, paras);
                    }
                    else
                    {
                        return (Form)Activator.CreateInstance(T);
                    }
                }
                catch(Exception ex)
                {
                    XtraMessageBox.Show("Chức Năng Này Đang Xây Dựng ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                finally
                {
                }
            
            return null;
        }

        private void barButtonItem4_ItemClick(object sender, ItemClickEventArgs e)
        {
            // trợ giúp
            OpenForm("frmAbout", null);
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            // đăng nhập lại
            Login frm = new Login(true);
            frm.Show();
            this.Hide();
        }

        private void barButtonItem2_ItemClick(object sender, ItemClickEventArgs e)
        {
            // đổi mật khẩu
            //frmThongtincanhan f = new frmThongtincanhan(_ID_nguoidung);
            //f.ShowDialog();
            OpenForm("frm_Doipass", null);
        }

        //private void barButtonItem23_ItemClick(object sender, ItemClickEventArgs e)
        //{
        //    // câu hỏi giảng viên
        //   OpenForm("frmCauhoi", new object[] { _ID_nguoidung });
        //}

        private void barButtonItem12_ItemClick(object sender, ItemClickEventArgs e)
        {
            // quản lý người dùng
            OpenForm("QLNguoiDung", null);
        }

        private void barButtonItem11_ItemClick(object sender, ItemClickEventArgs e)
        {
            // quản lý hộ ngheo
            OpenForm("frm_HoNgheo", null);
        }

        private void barButtonItem8_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenForm("frm_NguoiCC", null);
            // quản lý người có công 
        }

        private void barButton_baocao_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenForm("frm_BaoCao_search", null);
            // xuất báo cáo
        }

        private void barButtonItem13_ItemClick(object sender, ItemClickEventArgs e)
        {
            //vẽ biểu đồ
            OpenForm("frm_Chart", null);
        }

        private void barButtonItem14_ItemClick(object sender, ItemClickEventArgs e)
        {
            // xuất báo cáo ncc
            OpenForm("frm_Chartncc", null);
        }

        private void barButtonItem15_ItemClick(object sender, ItemClickEventArgs e)
        {
            // biểu đồ ncc
            OpenForm("frm_Chartncc", null);
        }
    }
}